package agriculture.controller;

import agriculture.bean.Order;
import agriculture.dto.OrderRequest;
import agriculture.dto.UpdateReceiverRequest;
import agriculture.service.OrderService;
import agriculture.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderRequest
     * @return
     */
    @PostMapping("/create")
    public Result<Order> createOrder(@RequestBody OrderRequest orderRequest) {
        Order order = orderService.createOrder(orderRequest);
        System.out.println(order);
        if (order != null ) {
            return new Result<>(200,"订单创建成功",order);
        }else {
            return new Result<>(500,"订单添加失败",null);
        }
    }

    /**
     * 根据用户Id获取该用户的所有订单信息
     * @param
     * @return
     */
    @GetMapping("/getOrderAllByUserId/{userId}")
    public  Result<List<Order>> getOrderById(@PathVariable Long userId) {
        List<Order> orders = orderService.selectOrderAllByUserId(userId);
        if (orders != null && orders.size() > 0) {
            return new Result<>(200,"个人订单查询成功",orders);
        }else {
            return new Result<>(500,"订单信息查询失败",null);
        }
    }

    /**
     * 添加收货人信息
     * @param orderNumber
     * @param updateReceiverRequest
     * @return
     */
    @PostMapping("/updateReceiver/{orderNumber}")
    public Result<Order> updateOrderInfo(@PathVariable String orderNumber, @RequestBody UpdateReceiverRequest updateReceiverRequest) {
        boolean result = orderService.updateOrderInfo(orderNumber, updateReceiverRequest);
        if (result) {
            return new Result<>(200,"更新收货人信息成功",null);
        }else {
            return new Result<>(500,"更新收货人信息失败",null);
        }
    }

    /**
     * 根据订单Id去获取对应订单的详细信息（订单主表）
     * @param orderNumber
     * @return
     */
    @GetMapping("/detail/{orderNumber}")
    public Result<Order> getOrderDetailByOrderNumber(@PathVariable String orderNumber) {
        System.out.println("845-----------");
        Order orderDetailByOrderNumber = orderService.getOrderDetailByOrderNumber(orderNumber);
        if (orderDetailByOrderNumber != null) {
            return new Result<>(200,"订单主表信息查询成功",orderDetailByOrderNumber);
        }else {
            return new Result<>(500,"订单主表信息查询失败", null);
        }
    }
}
